/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.skywalking.oap.server.cluster.plugin.zookeeper;

import com.google.common.base.Strings;
import java.util.*;
import org.apache.curator.x.discovery.*;
import org.apache.skywalking.oap.server.core.cluster.*;
import org.apache.skywalking.oap.server.core.remote.client.Address;
import org.apache.skywalking.oap.server.telemetry.api.TelemetryRelatedContext;
import org.slf4j.*;

/**
 * @author peng-yongsheng
 */
// ZookeeperCoordinator 同时实现了 ClusterRegister、ClusterNodesQuery 两个接口。
// 所有 OAP 实例在启动过程中都会通过 registerRemote() 方法将自身地址注册到 ZooKeeper 集群。在需要
// 获取所有 OAP 实例地址的时候，就可以通过 ZookeeperCoordinator 的 queryRemoteNodes() 方法进行查询。
public class ZookeeperCoordinator implements ClusterRegister, ClusterNodesQuery {
    private static final Logger logger = LoggerFactory.getLogger(ZookeeperCoordinator.class);

    private final ClusterModuleZookeeperConfig config;
    private final ServiceDiscovery<RemoteInstance> serviceDiscovery;
    private volatile ServiceCache<RemoteInstance> serviceCache;
    private volatile Address selfAddress;

    ZookeeperCoordinator(ClusterModuleZookeeperConfig config, ServiceDiscovery<RemoteInstance> serviceDiscovery) {
        this.config = config;
        this.serviceDiscovery = serviceDiscovery;
    }

    // 方法中会将 RemoteInstance 实例转换成 curator-x-discovery 扩展库中的 ServiceInstance 对象，并注册到 ZooKeeper。
    // 注意，这里传入的 RemoteInstance 实例中的 isSelf 标识都是 true，因为每个 OAP 服务只会暴露自身的地址（也只知道自身的地址）。
    @Override public synchronized void registerRemote(RemoteInstance remoteInstance) throws ServiceRegisterException {
        try {
            String remoteNamePath = "remote";
            if (needUsingInternalAddr()) {
                remoteInstance = new RemoteInstance(new Address(config.getInternalComHost(), config.getInternalComPort(), true));
            }

            // 将RemoteInstance对象转换成ServiceInstance对象
            ServiceInstance<RemoteInstance> thisInstance = ServiceInstance.<RemoteInstance>builder()
                .name(remoteNamePath)
                .id(UUID.randomUUID().toString()) // id是随机生成的UUID
                .address(remoteInstance.getAddress().getHost())
                .port(remoteInstance.getAddress().getPort())
                .payload(remoteInstance)
                .build();

            // 将ServiceInstance写入到Zookeeper中
            serviceDiscovery.registerService(thisInstance);

            // 创建ServiceCache，监听Zookeeper相应节点的变化，也方便后续的读取
            serviceCache = serviceDiscovery.serviceCacheBuilder()
                .name(remoteNamePath)
                .build();

            // 启动ServiceCache
            serviceCache.start();

            this.selfAddress = remoteInstance.getAddress();
            TelemetryRelatedContext.INSTANCE.setId(selfAddress.toString());
        } catch (Exception e) {
            throw new ServiceRegisterException(e.getMessage());
        }
    }

    // 方法中会查询 ServiceCache 获取全部的 ServiceInstance 对象，然后从每个 ServiceInstance 对象的 playload 字段中反序列化
    // 得到 RemoteInstance 实例（如果其中地址与当前 OAP 服务一致会将其 isSelf 字段设置为 true，标识本地 OAP 服务的地址）返回。
    @Override public List<RemoteInstance> queryRemoteNodes() {
        List<RemoteInstance> remoteInstanceDetails = new ArrayList<>(20);
        if (Objects.nonNull(serviceCache)) {
            List<ServiceInstance<RemoteInstance>> serviceInstances = serviceCache.getInstances();

            serviceInstances.forEach(serviceInstance -> {
                RemoteInstance instance = serviceInstance.getPayload();
                if (instance.getAddress().equals(selfAddress)) {
                    instance.getAddress().setSelf(true);
                } else {
                    instance.getAddress().setSelf(false);
                }
                remoteInstanceDetails.add(instance);
            });
        }
        return remoteInstanceDetails;
    }

    private boolean needUsingInternalAddr() {
        return !Strings.isNullOrEmpty(config.getInternalComHost()) && config.getInternalComPort() > 0;
    }
}
